package com.iflytek.sbd.demo.clone;

import lombok.Data;

/**
 * @program: sbd
 * @description: 深拷贝
 * @author: 陈英俊
 * @create: 2022-01-10 17:29
 */

@Data
public class DeepClone implements Cloneable {


    private Integer age;

    private CloneExample cloneExample;

    public DeepClone() {
    }

    public DeepClone(Integer age, CloneExample cloneExample) {
        this.age = age;
        this.cloneExample = cloneExample;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 这地方如果要实现深拷贝,就不能实现object.clone().因为该方法默认的浅拷贝, 下面使用clone实现了深拷贝
        // 说还可以使用序列化实现深拷贝,懒得搞了,后面有需要直接找工具类操作
        Object deep = null;
        deep = super.clone();
        DeepClone deepClone = (DeepClone) deep;
        deepClone.cloneExample = (CloneExample) cloneExample.clone();
        return deepClone;
    }
}
